package com.nec.recommend.common;

import java.io.Serializable;

/**
 * 封装结果集工具类
 *
 * @param <T>
 */
public class Result<T> implements Serializable {
    private final int status;
    private T data;
    private final String message;

    private Result(int status, T data, String message) {
        this.status = status;
        this.data = data;
        this.message = message;
    }

    private Result(ResultCode resultCode, T data) {
        this.status = resultCode.getCode();
        this.data = data;
        this.message = resultCode.getMessage();
    }

    public static <T> Result<T> ofStatus(ResultCode resultCode) {
        return new Result<T>(resultCode, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<T>(ResultCode.SUCCESS.getCode(), data, message);
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> failed(String message) {
        return new Result<T>(ResultCode.FAILED.getCode(), null, message);
    }

    public static <T> Result<T> failed() {
        return new Result<T>(ResultCode.FAILED, null);
    }

    public static <T> Result<T> valid(boolean isSuccess) {
        return new Result<T>(isSuccess ? ResultCode.SUCCESS : ResultCode.FAILED, null);
    }

    public int getStatus() {
        return status;
    }

    public T getData() {
        return data;
    }

    public String getMessage() {
        return message;
    }
}
